package com.lifeng.controller;

import com.lifeng.disconf.MySet;
import com.lifeng.disconf.service.TestService;
import com.lifeng.domain.User;
import com.lifeng.hystrix.CommandUserSafe;
import com.lifeng.serivce.UserService;
import com.mangofactory.swagger.annotations.ApiIgnore;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lifeng
 * 2017/12/11 16:36
 */
@RestController
@RequestMapping("/user")
@Api(value="user",description = "用户相关api")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TestService testService;
    @Autowired
    private MySet mySet;
    @RequestMapping(value = "/getUserInfo")
    @ApiOperation(value="通过ID获取用户信息",httpMethod="GET",notes="通过ID获取用户信息")
    public String getUser(@ApiParam(required=true,value="用户ID",name="id")
                          @RequestParam(value="id") Integer id,
                          @ApiParam(required=true,value="用户姓名",name="name")
                          @RequestParam(value="name") String name
                          ){
        System.out.println("用户ID:"+id+",姓名:"+name);
        String name1 = name;
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name1);
        return "name:"+name1;
    }

    @RequestMapping(value = "addUser",consumes = "application/json",produces = {"application/xml"})
    @ApiOperation(value="添加用户信息",httpMethod="POST",notes="添加用户信息json",response=String.class)
    public String addUser(@ApiParam(required=true,value="用户信息",name="params")
                           @RequestParam(value="params") String params){
        System.out.println("请求参数:"+params);
        return "<xml><code>success</code><msg>成功</msg></xml>";
    }

    @RequestMapping(value = "deleteUser")
    @ApiIgnore
    public String delete(){
       return "ok";
    }

    @RequestMapping("/test")
    public String test(){
        Object obj = new CommandUserSafe().execute();
        //String user = "sss";
        System.out.println(++i+"访问:"+(String)obj);
        return (String)obj;
    }

    public int i=0;

    @RequestMapping("/user/all")
    public List<User> findAll(){
        return userService.queryAll();
    }
    @RequestMapping("/myset")
    @ResponseBody
    public String myDisconf(){
        System.out.println(testService.getName());
        return mySet.getInfo();
    }

}
